package com.rem.hutool.my1Cloneable;

import cn.hutool.core.clone.CloneRuntimeException;
import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.clone.Cloneable;
import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 克隆
 *
 * @author Rem
 * @date 2020-08-18
 */

public class CloneDemo {


    public static void main(String[] args) {

        Cat cat = new Cat("miaomiao", 2);
        Cat cat2 = cat.clone();
        cat2.setName("mimi");
        System.err.println(cat);
        System.err.println(cat2);


        Dog dog = new Dog("wangwang", 3);
        Dog dog2 = dog.clone();
        dog2.setAge(5);
        System.err.println(dog);
        System.err.println(dog2);


        //深克隆
        Dog dog3 = ObjectUtil.clone(dog2);
        System.err.println(dog3);


    }


    /**
     * 对象克隆
     * 克隆出的对象参数不影响被克隆对象的参数
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class Cat implements Cloneable<Cat> {
        private String name;
        private int age;

        @Override
        public Cat clone() {
            try {
                return (Cat) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new CloneRuntimeException(e);
            }
        }
    }


    /**
     * 深度克隆
     * 泛型克隆类
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class Dog extends CloneSupport<Dog> {
        private String name;
        private int age;
    }


}
